// Copyright (C) 2025, ZWSOFT Co., LTD.(Guangzhou)
// Licensed under the MIT License. See LICENSE in the project root for license information.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TestClient.Domain;

namespace TestClient.ViewModel;
public partial class PrimitiveTestViewModel : PageViewModelBase
{
    [ObservableProperty]
    private string point1Id = "";
    [ObservableProperty]
    private string point2Id = "";
    [ObservableProperty]
    private string resultLineId = "";

    public IAsyncRelayCommand SelectPoint1Command { get; }
    public IAsyncRelayCommand SelectPoint2Command { get; }
    public IAsyncRelayCommand CreateLineCommand { get; }

    public PrimitiveTestViewModel(VFAdapterWrapper wrapper, ISnackNotifyService notifyService)
        : base(wrapper, notifyService)
    {
        SelectPoint1Command = CreateAsyncRpcCommand(SelectPoint1Async);
        SelectPoint2Command = CreateAsyncRpcCommand(SelectPoint2Async);
        CreateLineCommand = CreateAsyncRpcCommand(CreateLineAsync);
    }

    private async Task SelectPoint1Async()
    {
        EnsureConnected();
        Point1Id = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task SelectPoint2Async()
    {
        EnsureConnected();
        Point2Id = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task CreateLineAsync()
    {
        EnsureConnected();
        ResultLineId = await Task.Run(() =>
        {
            var pt1 = Wrapper.GetPointCoord(Point1Id);
            var pt2 = Wrapper.GetPointCoord(Point2Id);
            return Wrapper.CreateLine(pt1, pt2);
        });
    }
}
